package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.sxit.tdf.server.entity.Role;
import com.sxit.tdf.server.entity.bo.RoleInfoBo;
import com.sxit.tdf.server.entity.bo.RolePermissionBo;
import com.sxit.tdf.server.entity.bo.SimpleRoleListBo;
import com.sxit.tdf.server.entity.vo.DeleteRoleVo;
import com.sxit.tdf.server.entity.vo.InsertRoleVo;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IPermissionService;
import com.sxit.tdf.server.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 角色表 前端控制器
 *
 * @author 橙子高🍊
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/role")
@Tag(name = "RoleController", description = "角色前端控制器")
public class RoleController {

  @Autowired private IRoleService roleService;
  @Autowired private IPermissionService permissionService;

  @GetMapping("getRoleList")
  @Operation(summary = "获取全部的角色简易数据")
  @SaCheckLogin
  public R getRoleList() {

    List<SimpleRoleListBo> list = roleService.getAllSimpleRoleListBo();

    return R.ok().put("data", list);
  }

  @GetMapping("/getAllRoleList")
  @Operation(summary = "获取全部的角色数据")
  @SaCheckLogin
  @SaCheckPermission("PERMISSION:SELECT")
  public R getAllRoleList() {

    List<RoleInfoBo> roleList = roleService.getRoleList();

    return R.ok().put("data", roleList);
  }

  @GetMapping("/getRolePermission")
  @Operation(summary = "根据角色ID获取角色的权限信息")
  @SaCheckLogin
  public R getRolePermission(@PathParam("id") int id) {

    HashMap<String, List<RolePermissionBo>> rolePermissionList =
        permissionService.getRolePermissionList(id);

    return R.ok().put("data", rolePermissionList);
  }

  @Operation(summary = "修改角色信息")
  @PostMapping("/insertOrUpdateRole")
  @SaCheckLogin
  @SaCheckPermission(
      value = {"PERMISSION:UPDATE", "PERMISSION:INSERT"},
      mode = SaMode.OR)
  public R updateRole(@Valid @RequestBody InsertRoleVo insertRoleVo) {

    Role role = new Role();

    //  判断是更新还是新增
    if (insertRoleVo.getId() == null) {
      role.setRoleDesc(insertRoleVo.getRoleDesc());
      role.setSystemic(false);
      role.setRoleName(insertRoleVo.getRoleName());
      role.setPermissions(Arrays.toString(insertRoleVo.getPermission()));
      role.setDefaultPermissions("[]");
      role.setCreateTime(LocalDateTime.now());
      role.setUpdateTime(LocalDateTime.now());
      boolean save = roleService.save(role);
      if (save) {
        R.ok("添加成功");
      } else {
        R.error("添加失败");
      }
    } else {
      role.setId(insertRoleVo.getId());
      role.setRoleDesc(insertRoleVo.getRoleDesc());
      role.setUpdateTime(LocalDateTime.now());
      role.setRoleName(insertRoleVo.getRoleName());
      role.setPermissions(Arrays.toString(insertRoleVo.getPermission()));
      boolean update = roleService.updateById(role);
      System.out.println(role);
      if (update) {
        return R.ok("修改成功");
      } else {
        return R.error("修改失败");
      }
    }
    return R.ok();
  }

  @Operation(summary = "通过ID删除角色")
  @PostMapping("/deleteRole")
  @SaCheckPermission("PERMISSION:DELETE")
  public R deleteRole(@Valid @RequestBody DeleteRoleVo deleteRoleVo) {

    Boolean delete = roleService.deleteById(deleteRoleVo.getId());
    if (delete) {
      R.ok("删除成功");
    } else {
      R.error("删除失败");
    }

    return R.ok();
  }
}
